package club.pinea.handler.game;

import club.pinea.annotations.Head;
import club.pinea.interfaces.TCPMessage;
import club.pinea.proto.BasicProto;
import club.pinea.proto.CommandProto;
import club.pinea.proto.GameProto;
import club.pinea.send.MessageSend;
import club.pinea.service.RedisService;
import club.pinea.service.UserService;
import club.pinea.session.GlobalServerPlayerContainer;
import club.pinea.session.IOSession;
import club.pinea.session.ServerPlayer;
import club.pinea.util.GameUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
@Head(CommandProto.Command.C_S_DOUBLE_VALUE)
public class DoubleCardHandler implements TCPMessage<BasicProto.BaseMessage> {

    @Autowired
    private UserService userService;

    /**
     * 比倍倍数
     */
    public static int[] doublePower = new int[]{5, 10, 20};

    @Override
    public void doMessage(BasicProto.BaseMessage msg, IOSession session) throws Exception {
        ServerPlayer serverPlayer = GlobalServerPlayerContainer.getServerPlayer(session);
        GameUtil game = serverPlayer.getGameUtil();
        int doubleCardTime = game.getDoubleCardTime();
        Random random = new Random();
        int result = 0;
        if (doubleCardTime < 3) {
            game.setDoubleCardTime(doubleCardTime + 1);
            result = random.nextInt(2);
        }
        //直接获取当前比倍的结果
        if (result == 1) {
            // 保存数据库
            game.setGameScore(game.getGameScore() * doublePower[doubleCardTime]);
        } else {
            // 比倍输，更新记录
            game.setDoubleCardTime(0);
            game.setGameScore(0);
            //userService.updateSeatDoubleScore(gameScore, 0, seatId);//更新比倍信息
        }
        GameProto.DoubleCardResponse response = GameProto.DoubleCardResponse.newBuilder()
                .setDoubleWin(result)
                .setRes(MessageSend.successRes())
                .build();
        MessageSend.sendToUser(response, session);
    }
}
